<template>
    <div class="bigDeal-table-container">
        <div class="th-head th-td">
            <div class="th0 d-flex">
                <span class="mr-1">#</span>
                <Sort :sort="HotSort[0]" :call-fun="sortc.onDown.bind(sortc)"></Sort>
            </div>
            <div class="th1">{{ $t("message.Clever_Deal.a") }}</div>
            <div class="th2">{{ $t("message.Clever_Deal.c") }}</div>
            <div class="th3">{{ $t("message.Clever_Deal.b") }}</div>
            <div class="th4 d-flex justify-center">
                <div>{{ $t("message.Clever_Deal.ls") }}</div>
            </div>
            <div class="th5">{{ $t("message.Clever_Deal.ee") }}</div>
        </div>
        <div class="th-Empty" v-if="dataList.length == 0">
            <JEmpty></JEmpty>
        </div>
        <div class="th-table" v-else>
            <div class="th-row th-td" v-for="(item, index) in dataList" :key="index">
                <div class="th0">
                    {{ item.serialNumber }}
                </div>
                <div class="th1">
                    <JString class="coin-name" :title="item.name" :text="item.name" :max="20" @click="toBrowser(item)">
                    </JString>
                </div>
                <div class="th2">
                    <YIcons :items="filterArr(item?.top5Token || [])" :total="item.totalUsd"></YIcons>
                </div>
                <div class="th3">
                    <JNumber class="th-3-num" style="font-size: 14px;font-weight: 600;" :title="item.totalUsd"
                        :num="item.totalUsd" prefix="$">
                    </JNumber>
                </div>
                <div class="th4">
                    <div class="c-left">
                        <span class="c-title-4 c-name" @click="toToken(item)">{{ item.lastChangeToken.symbol }}</span>
                        <JNumber :title="item.lastChangeToken.balance" :num="item.lastChangeToken.balance" notbold>
                        </JNumber>
                    </div>
                    <JNumber class="c-right" :num="item.lastChangeToken.changeBalance" is-color ></JNumber>
                </div>
                <div class="th5">
                    <TimeTips :text="formatTimestamp(Number(item.lastChangeToken.lastActive))"> {{
                        formatTimeAgo(item.lastChangeToken.lastActive) }}</TimeTips>
                </div>
            </div>
        </div>

    </div>
</template>

<script setup lang="ts">
import type { Clever } from "../scripts/Clever.interface";
import { Bus, CONN_CHAIN_CLEVER } from "@/connetion/Bus";
import { useClever } from "@/stores/clever";
import { computed, onMounted, ref, onUnmounted, watch } from "vue";
import JEmpty from "@/components/j-empty/index.vue";
import JNumber from "@/components/j-number/index.vue";
import JString from "@/components/j-string/index.vue";
import YIcons from "@/components/y-icons/index.vue";
import { formatTimeAgo, formatTimestamp } from "@/comm/format";
import { get } from "@/comm/request";
import { URL_CAPITAL_SMART } from "@/comm/api";
import Sort from "@/components/j-sort/index.vue";
import { useCore } from "@/hooks/useCore";
import TimeTips from "@/components/timeTips/index.vue";
import { Sort as SortC, SortEnum } from "@/comm/sort";
import { useRouter } from "vue-router";
import { sortList as sorlTool } from "@/comm/tooler";
const sorts = ref([
    {
        key: 0,
        val: SortEnum.NONE
    },
    {
        key: 1,
        val: SortEnum.NONE
    },
    {
        key: 2,
        val: SortEnum.NONE
    }
]);
const sortc = new SortC(sorts);
const HotSort = sortc.sort;
const dataList = ref<any[]>([]);
const pages = ref({
    current: 1,
    size: 30,
})
const sortValue = ref(0)
const isMore = ref<Boolean>(true)

function setList() {
    console.log('调用 setList')
    const map = new Map();
    let list: any = []
    dataList.value.forEach(item => {
        const { capitalInfoId, lastChangeToken } = item;
        if (!map.has(capitalInfoId) || lastChangeToken?.lastActive > map.get(capitalInfoId)?.lastChangeToken?.lastActive) {
            map.set(capitalInfoId, item)
        }
    })
    list = [...map.values()]
    list.sort((a: any, b: any) => {
        return a.lastChangeToken?.lastActive - b.lastChangeToken?.lastActive < 0 ? 1 : -1;
    })
    dataList.value = list
}

onMounted(() => {
    updateData()
    Bus.on(CONN_CHAIN_CLEVER, (data: any) => {
        console.log('监听聪明钱推送！！！！', data)
        if (Array.isArray(data)) {
            data.forEach(i => {
                dataList.value.push(i)
            });
        } else {
            dataList.value.push(data)
        }
        selectSort()
    });
    document.getElementById('rout-page')?.addEventListener('scroll', scrollFn)
});
onUnmounted(() => {
    document.getElementById('rout-page')?.removeEventListener('scroll', scrollFn)
});
sortc.callFun = function (item){
    console.log('点击排序',item)
    sortValue.value = item.val;
    selectSort()
}
function selectSort(){
    if(sortValue.value == 0){
        setList()
    }
    if(sortValue.value == 1){
        sorlTool(dataList.value, "serialNumber",false);
    }
    if(sortValue.value == 2){
        sorlTool(dataList.value, "serialNumber",true);
    }
}
function scrollFn() {
    const element = document.getElementById('rout-page');
    if (element!.scrollTop + element!.clientHeight+20 >= element!.scrollHeight) {
        if (isMore.value) {
            pages.value.current += 1
            updateData()
        } else {
            console.log('没有更多了')
        }
    }
}
async function updateData() {
    console.log('加载数据')
    try {
        const res: any = await get(URL_CAPITAL_SMART, pages.value)
        console.log('链上聪明钱数据', res.data)
        let ary = res.data || [];
        if (ary.length > 0) {
            let temp: any = [...dataList.value, ...ary];
            dataList.value = temp;
            isMore.value = true
        } else {
            isMore.value = false
        }
        selectSort()
    } catch (error) {
        console.log(error);
    }
}
const toToken = function (item: any) {
    window.open("/token/" + item.lastChangeToken.contractAddress)
};
const toBrowser = function (item: any) {
    window.open("/entity/" + item.capitalInfoId)
};
const filterArr = (list:any) => {
    // console.log('过滤 list',list)
    if(Array.isArray(list)){
        return list.filter((i: any) => i?.balance)
    }else return []
}
</script>

<style lang="scss" scoped>
.bigDeal-table-container {
    display: flex;
    flex-direction: column;
    width: 792px;

    .th-head {
        color: var(--color);
        font-size: 12px;
    }

    .th-Empty {
        position: relative;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .th-td {
        height: var(--row-height);
        display: flex;
        align-items: center;
        border-top: 1px solid var(--border);
        padding: 0 15px 0 15px;

        &:last-child {
            border-bottom: 1px solid var(--border);
        }
        .th0{
            flex: 0.3;
            font-size: 14px;
            color: var( --sub);
           
        }
        .th1 {
            display: flex;
            flex: 1.8;

            .coin-name {
                font-size: 14px;

                &:hover {
                    cursor: pointer;
                    text-decoration: underline;
                }

                .coin-1 {
                    color: #FFFFFF;
                }

                .coin-2 {
                    color: var(--color-2);
                }
            }

        }

        .th2 {
            flex: 1.1;
        }

        .th3 {

            flex: 1.2;


            .th-3-num {
                font-size: 14px;
                font-weight: 600;
            }
        }

        .th4 {
            display: flex;
            align-items: center;
            // overflow: hidden;
            flex: 2;
            gap: 10px;
            .c-left {
                display: flex;
                justify-content: flex-end;
                align-items: center;
                overflow: hidden;
                flex: 0.6;
                .c-name {
                    color: #b1b6c6;
                    margin-right: 4px;

                    &:hover {
                        text-decoration: underline;
                    }
                }
            }

            .c-right {
                flex: 0.4;
            }


        }

        .th5 {
            flex: 1.1;
            display: flex;
            justify-content: flex-end;
        }

        .th6 {
            flex: 1;
            display: flex;
            justify-content: flex-end;
        }
    }
}
</style>
